<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyValue" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search()">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="pageinfo.departments" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="部门名称" width="120" sortable>
			</el-table-column>
			<el-table-column prop="createTime" label="创立时间" width="120" sortable>
			</el-table-column>
			<el-table-column prop="updateTime" label="修改时间" width="120" sortable>
			</el-table-column>
			<el-table-column prop="employee.username" label="部门经理" width="100" sortable>
			</el-table-column>
			<el-table-column prop="department.name" label="上级部门" width="120" sortable>
			</el-table-column>
			<el-table-column prop="path" label="部门路径" min-width="120" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" min-width="100" sortable>
				<template slot-scope="scope">
					<span v-if="scope.row.state==1" style="color: limegreen">正常</span>
					<span v-else style="color: red">关闭</span>
				</template>
			</el-table-column>
			<el-table-column prop="intro" label="部门描述" min-width="180" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
						   :current-page="query.currentPage" :page-size="10" :total="pageinfo.total" style="float:right;">
			</el-pagination>
		</el-col>


		<!--新增界面-->
		<el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="部门名称" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="部门主管" prop="managet_id">
					<el-select v-model="saveForm.employee.id" placeholder="请选择">
						<el-option
								v-for="item in employees"
								:key="item.id"
								:label="item.username"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="上级部门" prop="parent_id">
					<el-cascader
							v-model="saveForm.department"
							:options="departmentTree"
							:props="{ checkStrictly: true ,
									label:'name',
									value:'id'}"
							clearable></el-cascader>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="saveForm.state">
						<el-radio class="state" :label="1">启用</el-radio>
						<el-radio class="state" :label="0">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="描述">
					<el-input type="textarea" v-model="saveForm.intro"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	// import util from '../../common/js/util'
	// //import NProgress from 'nprogress'
	// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				pageinfo:{
					departments: [],
					total: 0,
				},
				page: 1,
				listLoading: false,
				sels: [],//列表选中列
				query:{
					currentPage:1,
					pageSize:10,
					keyValue:""
				},
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				saveFormVisible: false,//新增界面是否显示
				addLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入部门名称', trigger: 'blur' }
					]
				},
				departmentTree:[],
				employees:[],
				//新增界面数据
				saveForm: {
					name: '',
					intro: '',
					employee:{
						id:null
					},
					department:{
						id:null
					},
					state:''
				}

			}
		},
		methods: {
			getDepartmentTree(){
				this.$http.get("/department/tree").then(result=>{
					result=result.data;
					this.departmentTree=result.object;
				}).catch(result=>{
					this.$message({
						message: '上级部门查询失败',
						type: 'error'
					});
				})
			},
			getEmployee(){
				this.$http.get("/employee").then(result=>{
					result=result.data;
					this.employees=result.object;
					console.log(this.employees)
				}).catch(result=>{
					this.$message({
						message: '网络波动',
						type: 'error'
					});
				})
			},
			selsChange(sels){
				this.sels=sels;
			},
			handleCurrentChange(val) {
				this.query.currentPage = val;
				this.getDepartment();
			},
			//获取部门列表
			getDepartment() {
				this.$http.post("/department",this.query).then(result=>{
					result=result.data;
					console.log(result," department部门列表");
					if (result.success){
						this.pageinfo.total=result.object.size;
						this.pageinfo.departments=result.object.rows;
					}else {
						this.$message({
							message: '网络波动'+result.message,
							type: 'error'
						});
					}


				}).catch(()=>{
				})
			},
			//查找
			search(){
				this.query.currentPage=1;
				this.getDepartment();
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading=true;
					this.$http.delete("/department/"+row.id).then(result=>{
						this.listLoading=false;
						result=result.data;
						if (result.success){
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.query.currentPage=1;
							this.getDepartment();
						}else {
							this.$message({
								message: '删除失败',
								type: 'error'
							});
						}
					})
				}).catch(() => {
					this.listLoading=false;
					this.$message({
						message: '取消请求',
						type: 'error'
					});
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);

				if (!this.saveForm.department){
					this.saveForm.department={id:null}
				}else{
					let path=row.path;
					var setPath=path.split("/");
					var temp=[];
					for (var i=1;i<setPath.length-1;i++){
						temp.push(parseInt(setPath[i]));
					}
					this.saveForm.department=temp;
					console.log(this.saveForm," path")
				}
				if (!this.saveForm.employee.id){
					this.saveForm.employee={id:null}
				}
				this.getEmployee();
				this.getDepartmentTree();
			},
			//显示新增界面
			handleAdd: function () {
				this.saveFormVisible = true;
				this.saveForm = {
					name: '',
					intro: '',
					employee:{
						id:null
					},
					department:{
						id:null
					},
					state:''
				};
				this.getEmployee();
				this.getDepartmentTree();
			},
			//新增或编辑
			addSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							this.saveFormVisible=true;
							//NProgress.start();
							let para = Object.assign({}, this.saveForm);
							let setr=para.department;
							para.department={id:setr[setr.length-1]};
							console.log(para,"新增处断点")
							this.saveFormVisible = false;
							this.$http.put("/department",para).then(result=>{
								this.addLoading = false;
								result=result.data;
								if (result.success){
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.getDepartment();
								}else {
									this.$message({
										message: '提交失败',
										type: 'error'
									});
								}
							})
						});
					}
				});
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					console.log(ids);
					this.$http.patch("/department",ids).then(result=>{
						this.listLoading=false;
						result=result.data;
						if (result.success){
							this.$message({
								message: '批量删除成功',
								type: 'success'
							});
							this.query.currentPage=1;
							this.getDepartment();
						}else {
							this.$message({
								message: '批量删除失败',
								type: 'error'
							});
						}
					}).catch(result=>{
						this.listLoading=false;
						this.$message({
							message: '网络故障请重新选择',
							type: 'error'
						});
					})
				}).catch(() => {
					this.listLoading=false;
					this.$message({
						message: '请重新选择1',
						type: 'success'
					});
				});
			}
		},
		mounted() {
			this.getDepartment();
		}
	}

</script>

<style scoped>

</style>